package com.jgsu.farm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jgsu.farm.entity.StressCategory;
import com.jgsu.farm.enums.stresscategoryenums.MainType;
import com.jgsu.farm.enums.stresscategoryenums.SubType;
import com.jgsu.farm.exception.DataAddException;
import com.jgsu.farm.exception.DataNotExistException;
import com.jgsu.farm.mapper.StressCategoryMapper;
import com.jgsu.farm.mapper.sql.StressCategoryMapperSql;
import com.jgsu.farm.service.StressCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StressCategoryServiceImpl implements StressCategoryService {


    @Autowired
    StressCategoryMapperSql stressCategoryMapperSql;
    @Autowired
    StressCategoryMapper stressCategoryMapper;

//--------------------------------------------------------------------------------

    @Override
    public PageInfo<StressCategory> getStressCategories(Integer page, Integer pagesize) {
        PageHelper.startPage(page, pagesize);
        List<StressCategory> stressCategoryList = stressCategoryMapper.selectAll();
        PageInfo<StressCategory> pageInfoStressCategoryList = new PageInfo<StressCategory>(stressCategoryList);
        return pageInfoStressCategoryList;
    }

    @Override
    public void add(StressCategory stressCategory) {

        // 一级胁迫类型：非生物胁迫
        boolean mainTypeAbiotic = MainType.ABIOTIC_STRESS.getName().equals(stressCategory.getMaintype());

        // 一级胁迫类型：生物胁迫
        boolean mainTypeBiological = MainType.BIOLOGICAL_STRESS.getName().equals(stressCategory.getMaintype());

        // 二级胁迫类型：非生物胁迫
        boolean subTypeAbiotic = (SubType.LIGHT_STRESS.getName().equals(stressCategory.getSubtype()) ||
                SubType.METAL_STRESS.getName().equals(stressCategory.getSubtype()) ||
                SubType.NUTRIENT_STRESS.getName().equals(stressCategory.getSubtype()) ||
                SubType.TEMPERATURE_STRESS.getName().equals(stressCategory.getSubtype()) ||
                SubType.WATER_STRESS.getName().equals(stressCategory.getSubtype()));

        // 二级胁迫类型：生物胁迫
        boolean subTypeBiological = (SubType.DISEASE_STRESS.getName().equals(stressCategory.getSubtype()) ||
                SubType.PEST_STRESS.getName().equals(stressCategory.getSubtype()) ||
                SubType.GRASS_STRESS.getName().equals(stressCategory.getSubtype()));

        if (mainTypeAbiotic && subTypeAbiotic) {
            stressCategoryMapper.insert(stressCategory);
        } else if (mainTypeBiological && subTypeBiological) {
            stressCategoryMapper.insert(stressCategory);
        } else {
            throw new DataAddException("604", "插入的胁迫类型数据不存在");
        }
    }

    @Override
    public void delete(Integer stressid) {
        Integer i = stressCategoryMapper.deleteByPrimaryKey(stressid);
        if (i == 0 || i == null) {
            throw new DataNotExistException("603", "不存在该id,胁迫类型删除失败");
        }
    }

    @Override
    public void update(StressCategory stressCategory) {
        StressCategory stressCategoryId = stressCategoryMapper.selectByPrimaryKey(stressCategory.getStressid());
        if (stressCategoryId != null) {
            stressCategoryMapper.updateByPrimaryKey(stressCategory);
        } else {
            throw new DataNotExistException("603", "不存在该id,胁迫类型更新失败");
        }
    }

    //--------------------------------------------------------------------------------

    @Override
    public Integer findStressId(String stressname) {
        Integer stressid = stressCategoryMapperSql.selectByName(stressname);
        return stressid;
    }
}
